package com.example.commentservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commentservice.entity.Comment;
import com.example.commentservice.entity.Commentvo;
import com.example.commentservice.service.IArticleservice;
import com.example.commentservice.service.ICommentService;
import com.example.commentservice.service.IUserservice;
import com.example.common.Ajaxresult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-02-21
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ICommentService iCommentService;
    @Autowired
    private IUserservice iUserservice;
    @Autowired
    private IArticleservice iArticleservice;
    @RequestMapping("/listbyaid")
    public Ajaxresult listbyaid(Long aid){
          if(aid==null||aid<=0){
              return Ajaxresult.fail(-1,"参数错误");
          }
        QueryWrapper<Comment>queryWrapper=new QueryWrapper<>();
          queryWrapper.eq("aid",aid);
          List<Comment> commentList=iCommentService.list(queryWrapper);
          List<Commentvo>result=new ArrayList<>();
          commentList.stream().forEach(comment -> {
              Commentvo commentvo=new Commentvo();
              commentvo.setContent(comment.getContent());
              commentvo.setUid(comment.getUid());
              commentvo.setAid(comment.getAid());
              commentvo.setCid(comment.getCid());
              commentvo.setUsername(iUserservice.getusername(comment.getUid()));
              result.add(commentvo);
          });
          Collections.sort(result,new Comparator<Commentvo>(){
              public int compare(Commentvo o1,Commentvo o2){
                  return (int)(o2.getCid()-o1.getCid());
              }
          });
          return Ajaxresult.success(result);
    }
    @RequestMapping("/delbyaid")
    public boolean delbyaid(Integer aid){
        if(aid==null||aid<=0){
            return false;
        }
        QueryWrapper<Comment>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("aid",aid);
        boolean result=iCommentService.remove(queryWrapper);
        return  result;
    }
    @RequestMapping("/add")
    public Ajaxresult add(Comment comment, @RequestHeader("uid")Long uid){
        if(comment==null||comment.getAid()==null||comment.getAid()<=0||!StringUtils.hasLength(comment.getContent())
        ||uid==null||uid<=0){
            return Ajaxresult.fail(-1,"参数有误");
        }
        comment.setUid(uid);
        //添加评论到数据库
       boolean result= iCommentService.save(comment);
        return Ajaxresult.success(result);

    }
    @RequestMapping("/del")
    public Ajaxresult del(Long cid,@RequestHeader("uid")Long uid){
        if(cid==null||cid<=0||uid==null||uid<=0){
            return Ajaxresult.fail(-1,"参数错误");
        }
            Comment comment=iCommentService.getById(cid);
        if(comment==null||comment.getUid()<=0){
            return Ajaxresult.fail(-1,"参数错误");
        }
        boolean isdel=false;
        //如果文章评论是自己的可以删除
        if(comment.getUid()==uid){
            isdel=true;
        }
        //如果登陆用户是这条评论的文章的作者可以删除
        Long articleuid=iArticleservice.getuidbyaid(comment.getAid());
        if(articleuid==uid){
            isdel=true;
        }
        if(!isdel){
            return Ajaxresult.fail(-2,"没有权限删除");
        }
        boolean result=iCommentService.removeById(cid);
        return Ajaxresult.success(isdel);

    }


}
